package com.apestech.api.workflow.lib.element;

import com.alibaba.druid.pool.DruidDataSource;
import com.apestech.framework.util.ReflectUtil;
import org.flowable.engine.*;
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;

import javax.sql.DataSource;
import java.lang.reflect.Field;
import java.sql.Connection;

/**
 * @author hup
 * @create 2018-08-17-9:32
 */
public class WorkProcessEngine implements ProcessEngine {

    private ProcessEngine engine;
    private Connection conn;

    public WorkProcessEngine(ProcessEngine engine){
        this.engine=engine;
        this.engine.getProcessEngineConfiguration().setActivityFontName("宋体");
        this.engine.getProcessEngineConfiguration().setLabelFontName("宋体");
        this.engine.getProcessEngineConfiguration().setAnnotationFontName("宋体");
    }

    public Connection getConnection() {
        if(conn==null){
            conn=this.engine.getProcessEngineConfiguration().
                    getSqlSessionFactory()
                    .openSession(
                            engine.getProcessEngineConfiguration()
                                    .getSqlSessionFactory()
                                    .getConfiguration()
                                    .getDefaultExecutorType())
                    .getConnection();
        }
        return conn;
    }

    public DruidDataSource getDataSource(){
        Object ds=engine.getProcessEngineConfiguration().getDataSource();
        if(ds instanceof  TransactionAwareDataSourceProxy){
            Object ds1=((TransactionAwareDataSourceProxy) ds).getTargetDataSource();
            if(ds1 instanceof DruidDataSource){
                return (DruidDataSource) ds1;
            } else {
                return (DruidDataSource) ReflectUtil.getPrivateField(ds1, "realDataSource");
            }
        } if(ds instanceof DruidDataSource){
            return (DruidDataSource) ds;
        }
        return null;
    }

    public ProcessEngine getEngine() {
        return engine;
    }

    @Override
    public String getName() {
        return engine.getName();
    }

    @Override
    public void close() {
        engine.close();
    }

    @Override
    public RepositoryService getRepositoryService() {
        return engine.getRepositoryService();
    }

    @Override
    public RuntimeService getRuntimeService() {
        return engine.getRuntimeService();
    }

    @Override
    public FormService getFormService() {
        return engine.getFormService();
    }

    @Override
    public TaskService getTaskService() {
        return engine.getTaskService();
    }

    @Override
    public HistoryService getHistoryService() {
        return engine.getHistoryService();
    }

    @Override
    public IdentityService getIdentityService() {
        return engine.getIdentityService();
    }

    @Override
    public ManagementService getManagementService() {
        return engine.getManagementService();
    }

    @Override
    public DynamicBpmnService getDynamicBpmnService() {
        return engine.getDynamicBpmnService();
    }

    @Override
    public ProcessEngineConfiguration getProcessEngineConfiguration() {
        return engine.getProcessEngineConfiguration();
    }
}
